package com.roncoo.education.course.service.api.biz;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.enums.StatusIdEnum;
import com.roncoo.education.course.dao.OptionsDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.Options;
import com.roncoo.education.course.dao.impl.mapper.entity.OptionsExample;

/**
 * 课程分类
 *
 * @author wujing
 */
@Component
@CacheConfig(cacheNames = {"course"})
public class ApiOptionsBiz {

    @Autowired
    private OptionsDao dao;

    /**
     * 获取课程分类列表
     *
     * @return
     * @author wuyun
     */
    @Cacheable
    public Result<Map<String, List<Options>>> list() {        
        Map<String, List<Options>> optionsMap = mapList();        
        return Result.success(optionsMap);
    }
    
    @Cacheable
    public Map<String, List<Options>> mapList() {
        // 根据分类类型、层级查询可用状态的课程分类集合
        OptionsExample example = new OptionsExample();
        example.createCriteria().andStatusIdEqualTo(StatusIdEnum.YES.getCode());
        example.setOrderByClause("category asc, sort asc, id desc ");
        List<Options> options = dao.listByExample(example);
        
        Map<String, List<Options>> optionsMap = options.stream().collect(Collectors.groupingBy(Options::getCategory, Collectors.toList()));        
        return optionsMap;
    }

    public Experiment setOptionNameByOptionValue(Experiment experiment) {
    	if (experiment != null) {
    		List<Experiment> list = new ArrayList<Experiment>();
    		list.add(experiment);
    		
    		list = setOptionNameByOptionValue(list);
    		
    		return list.get(0);		
    	}
	
    	return experiment;
    }
    
    public List<Experiment> setOptionNameByOptionValue(List<Experiment> list) {
		if (list != null && list.size() >= 1) {
			Map<String, List<Options>> mapList = mapList();
			Map<String, String> zhuanyeMap = mapList.get("zhuanye").stream().collect(Collectors.toMap(Options::getOptionValue, Options::getOptionName));
			Map<String, String> kechengjibieMap = mapList.get("kechengjibie").stream().collect(Collectors.toMap(Options::getOptionValue, Options::getOptionName));
			Map<String, String> kechengleixingMap = mapList.get("kechengleixing").stream().collect(Collectors.toMap(Options::getOptionValue, Options::getOptionName));
			Map<String, String> shiyanleixingMap = mapList.get("shiyanleixing").stream().collect(Collectors.toMap(Options::getOptionValue, Options::getOptionName));
			
    		for(Experiment exp: list) {
    			if (StringUtil.isNotBlank(exp.getZhuanye())) {
    				exp.setZhuanye(zhuanyeMap.get(exp.getZhuanye()));
    			}
    			if (StringUtil.isNotBlank(exp.getKechengjibie())) {
    				exp.setKechengjibie(kechengjibieMap.get(exp.getKechengjibie()));
    			}
    			if (StringUtil.isNotBlank(exp.getKechengleixing())) {
    				exp.setKechengleixing(kechengleixingMap.get(exp.getKechengleixing()));
    			}
    			if (StringUtil.isNotBlank(exp.getShiyanleixing())) {
    				exp.setShiyanleixing(shiyanleixingMap.get(exp.getShiyanleixing()));
    			}
    		}            			
		}
		
		return list;
    }
}
